package random

import "sync/atomic"

var ID_DICT = DICT_FIXED()

func D4() int {
	return Natural(1, 4)
}

func D6() int {
	return Natural(1, 6)
}

func D8() int {
	return Natural(1, 8)
}

func D12() int {
	return Natural(1, 12)
}

func D20() int {
	return Natural(1, 20)
}

func D100() int {
	return Natural(1, 100)
}

func Guid() string {
	var (
		pool = "abcdefABCDEF1234567890"
		guid = String(pool, 8, 8) + "-" +
			String(pool, 4, 4) + "-" +
			String(pool, 4, 4) + "-" +
			String(pool, 4, 4) + "-" +
			String(pool, 12, 12)
	)
	return guid
}

func Uuid() string {
	return Guid()
}

/*
	Id

随机生成一个 18 位身份证。
[身份证](http://baike.baidu.com/view/1697.htm#4)

	地址码 6 + 出生日期码 8 + 顺序码 3 + 校验码 1

[《中华人民共和国行政区划代码》国家标准(GB/T2260)](http://zhidao.baidu.com/question/1954561.html)
*/
func Id() string {
	var (
		id   []byte
		sum  int = 0
		rank     = []byte{
			7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2,
		}
		last = []byte{
			'1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2',
		}
	)

	id = []byte(Pick(ID_DICT, 0, len(ID_DICT)).Id +
		Date("yyyyMMdd") +
		String("number", 3, 3))

	for i := 0; i < len(id); i++ {
		sum += int(id[i] * rank[i])
	}
	id = append(id, last[sum%11])

	return string(id)
}

func Increment[T Number]() func(T) T {
	var (
		key int64 = 0
	)
	return func(step T) T {
		if step == 0 {
			step = 1
		}
		return T(atomic.AddInt64(&key, int64(step)))
	}
}

var increment = Increment[int]()

func Inc(step int) int {
	return increment(step)
}
